package hello.app.model;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class HelloSetting {
	private boolean serviceEnabled;
	private boolean evalEnabled;
	private boolean googleEnabled;
	private int history_counter;
	private String appVersion;
	private int postion;
	private boolean superUser;
	private int user_id;
	public static final int TOAST = 0;
	public static final int DIALOG = 1;
	public static final int HIGH = 0;
	public static final int LITTLE_HIGH = 1;
	public static final int CENTER = 2;
	public static final int LITTLE_LOW = 3;
	public static final int LOW = 4;
	public static int[] DISPLAY_POSTION = {-250,-125,0,125,250};
	public static final int HIGH_POSTION = -250;
	public static final int LITTLE_HIGH_POSTION = -125;
	public static final int CENTER_POSTION = 0;
	public static final int LITTLE_LOW_POSTION = 125;
	public static final int LOW_POSTION = 250;
	public static final int DEFAULT_USER_ID = 0;

	public static final int DEFAULT_HISTORY_COUNT = 0;
	public static final int HISTORY_COUNT_RECCOMEND = 10;
	public static final int HISTORY_COUNT_WARNING = 1000;
	public static final int HISTORY_COUNT_STOP = 1050;

	public static final int MAX_HISTORY_DISPLAY_COUNT = 200;
	private SharedPreferences mPref;

	public enum DataType{
		SERVICE,
		EVAL_CONTACT,
		GOOGLE,			//Googleでの電話番号検索
		SUPER_USER,
		DISPLAY_POSTION,
		USER_ID,
		HISTORY_COUNTER,
		APP_VERSION,
		SHOW_COMMMENT_DLG,
		COMMENT_LINE_NUMBER,
		HISTORY_DISPLAY_COUNT
	}

	HelloSetting(Context context){
		mPref = PreferenceManager.getDefaultSharedPreferences(context);
		superUser = mPref.getBoolean(DataType.SUPER_USER.name(), false);
		user_id = mPref.getInt(DataType.USER_ID.name(), DEFAULT_USER_ID);
		history_counter = mPref.getInt(DataType.HISTORY_COUNTER.name(), DEFAULT_HISTORY_COUNT);
	}

	public void write(DataType service, String value){
		Editor e = mPref.edit();
		String key = service.name();
		boolean change = true;
		switch(service){
		case APP_VERSION:
			appVersion = value;
			break;
		default:
			change = false;
			break;
		}
		if(change){
			e.putString(key, value);
			e.commit();
		}
	}

	public void write(DataType service, int value){
		Editor e = mPref.edit();
		String key = service.name();
		boolean change = true;
		switch(service){
		case DISPLAY_POSTION:
			postion = value;
			break;
		case USER_ID:
			user_id = value;
			break;
		default:
			change = false;
			break;
		}
		if(change){
			e.putInt(key, value);
			e.commit();
		}
	}

	public void write(DataType service, boolean value){
		Editor e = mPref.edit();
		String key = service.name();
		boolean change = true;
		switch(service){
		case SERVICE:
			serviceEnabled = value;
			break;
		case EVAL_CONTACT:
			evalEnabled = value;
			break;
		case GOOGLE:
			googleEnabled = value;
			break;
		case SUPER_USER:
			superUser = value;
			break;
		default:
			change = false;
			break;
		}
		if(change){
			e.putBoolean(key, value);
			e.commit();
		}
	}

	public boolean isServiceEnabled(){
		return mPref.getBoolean(DataType.SERVICE.name(), true);
	}

	public boolean isEvalEnabled(){
		return mPref.getBoolean(DataType.EVAL_CONTACT.name(), true);
	}

	public boolean isGoogleEnabled(){
		return mPref.getBoolean(DataType.GOOGLE.name(), false);
	}

	public boolean isShowCommentDlgEnabled(){
		return mPref.getBoolean(DataType.SHOW_COMMMENT_DLG.name(), false);
	}

	public boolean isSuperUser(){
		return superUser;
	}

	public int getHistoryDisplayCount(){
		return Integer.valueOf(mPref.getString(DataType.HISTORY_DISPLAY_COUNT.name(), "100"));
	}

	public int getDisplayPostion(){
		return Integer.valueOf(mPref.getString(DataType.DISPLAY_POSTION.name(), Integer.toString(CENTER)));
	}

	public int getCommentLineNumber(){
		return Integer.valueOf(mPref.getString(DataType.COMMENT_LINE_NUMBER.name(), "1"));
	}

	public int getUserId(){
		return user_id;
	}

	public boolean isUpdatedApplication(String appVersion){
		return !mPref.getString(DataType.APP_VERSION.name(), "").equals(appVersion);
	}

	public int addHistoryCounter(){
		int counter = getHistoryCounter();
		counter++;
		Editor e = mPref.edit();
		e.putInt(DataType.HISTORY_COUNTER.name(),counter);
		e.commit();
		return counter;
	}

	public int getHistoryCounter(){
		return mPref.getInt(DataType.HISTORY_COUNTER.name(), DEFAULT_HISTORY_COUNT);
	}
}
